/*
 *  Licensed to PECATS under one or more contributor license agreements below:
 *  1. http://www.mozilla.org/MPL/MPL-1.1.html
 *  2. http://www.gnu.org/licenses/gpl-2.0.html
 *  3. http://www.gnu.org/licenses/lgpl-2.1.html
*/

/* **********************************************************
*  Class to parse an XML file
*  Name: ParseXMLFile.java
*  Author: www.pecats.com
*  Date: Sep 15, 2008
************************************************************/

package com.pecats.batch.util;

import java.io.PrintWriter;

import org.xml.sax.Attributes;
import org.xml.sax.Parser;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.ParserAdapter;
import org.xml.sax.helpers.ParserFactory;
import org.xml.sax.helpers.XMLReaderFactory;

import com.pecats.batch.data.ConfigData;

/**
 * Used to parse an XML File
 */

public class ParseXMLFile extends DefaultHandler
{

/** Default parser name. */
    protected static final String DEFAULT_PARSER_NAME = "org.apache.xerces.parsers.SAXParser";
	String tempVal;

	public ParseXMLFile()
	{
	}


	public void parseXML(String xmlFileName)
	{
		    XMLReader parser = null;

		    ParseXMLFile simplexml = new ParseXMLFile();
		
                try 
		    {
	            // create parser
		        parser = XMLReaderFactory.createXMLReader(DEFAULT_PARSER_NAME);
            

      		// parse file
	              parser.setContentHandler(simplexml);
	       	  parser.setErrorHandler(simplexml);

                    parser.parse(xmlFileName);
		    }

		    catch (SAXParseException e) 
		    {
                // ignore
                }

                catch (Exception e) 
		    {
                    System.err.println("error: Unable to instantiate parser ("+DEFAULT_PARSER_NAME+")");
                } 
	}

	
	public void startElement(String namespaceURI,
                             String localName,
                             String qualifiedName,
                             Attributes attributes)
                throws SAXException 
      {

     }


    private String getName(String s1, String s2) {
        if (s1 == null || "".equals(s1)) return s2;
        else return s1;
    }

   	public void endElement (String uri, String name, String qName)
    	{
		if (qName.equalsIgnoreCase("raw-logs-dir")) {
			ConfigData.rawlogsdir = tempVal;
		}
		if (qName.equalsIgnoreCase("pool-size")) {
			ConfigData.poolsize = Integer.parseInt(tempVal);
		}
		if (qName.equalsIgnoreCase("processed-logs-dir")) {
			ConfigData.processedlogsdir = tempVal;
		}
		if (qName.equalsIgnoreCase("pool-timeout")) {
			ConfigData.pooltimeout = Integer.parseInt(tempVal);
		}
		if (qName.equalsIgnoreCase("output-dir")) {
			ConfigData.outputdir = tempVal;
		} 
		if (qName.equalsIgnoreCase("server-name")) {
			ConfigData.servername = tempVal;
		}
		if (qName.equalsIgnoreCase("mail-properties")) {
			ConfigData.mailproperties = tempVal;
		}	
		if (qName.equalsIgnoreCase("hostname")) {
			ConfigData.hostname = tempVal;
		}	
		if (qName.equalsIgnoreCase("port")) {
			ConfigData.port = tempVal;
		}	
	}

 	public void startDocument ()
    	{
		
    	}

    	public void endDocument ()
    	{
		
    	}

	public void characters(char[] ch, int start, int length) throws 		SAXException 
	{
			tempVal = new String(ch,start,length);
	}

}